package java8NewFeatures.dateDomo;

import java.time.*;

/**
 * @author ldy
 * @data 2022/11/9
 * @description
 * Duration:用于计算两个“时间”间隔。
 *
 * Period:用于计算两个“日期”间隔 。
 *
 */
public class DurationAndPeriodDemo {
    public static void main(String[] args) {
        Instant instant_1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant instant_2 = Instant.now();

        Duration duration = Duration.between(instant_1, instant_2);
        System.out.println(duration.toMillis());
// 运行结果：1000

        LocalTime localTime_1 = LocalTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime localTime_2 = LocalTime.now();

        System.out.println(Duration.between(localTime_1, localTime_2).toMillis());
// 运行结果：1000
        LocalDate localDate_1 = LocalDate.of(2018,9,    10);
        LocalDate localDate_2 = LocalDate.now();

        Period period = Period.between(localDate_1, localDate_2);
        System.out.println(period.getYears());      // 运行结果：1
        System.out.println(period.getMonths());     // 运行结果：1
        System.out.println(period.getDays());       // 运行结果：18
    }
}
